//
//  COHBDNavigationBar.m
//  CoworkClient
//
//  Created by YueWen on 2018/10/15.
//  Copyright © 2018 YueWen. All rights reserved.
//

#import "COHBDNavigationBar.h"

@implementation COHBDNavigationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]){
        
        self.shadowImageViewHeight = 0.5;
        self.shadowImageViewOffsetY = 0.5;
    }
    
    return self;
}


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) {
        return nil;
    }
    
    UIView *view = [super hitTest:point withEvent:event];
    NSString *viewName = [[[view classForCoder] description] stringByReplacingOccurrencesOfString:@"_" withString:@""];
    
    if (view && ([viewName isEqualToString:@"HBDNavigationBar"] ||[viewName isEqualToString:@"COHBDNavigationBar"])) {
        for (UIView *subview in self.subviews) {
            NSString *viewName = [[[subview classForCoder] description] stringByReplacingOccurrencesOfString:@"_" withString:@""];
            NSArray *array = @[ @"UINavigationItemButtonView" ];
            if ([array containsObject:viewName]) {
                CGPoint convertedPoint = [self convertPoint:point toView:subview];
                CGRect bounds = subview.bounds;
                if (bounds.size.width < 80) {
                    bounds = CGRectInset(bounds, bounds.size.width - 80, 0);
                }
                if (CGRectContainsPoint(bounds, convertedPoint)) {
                    return view;
                }
            }
        }
    }
    
    NSArray *array = @[ @"UINavigationBarContentView", @"HBDNavigationBar",@"COHBDNavigationBar"];
    if ([array containsObject:viewName]) {
        if (self.backgroundImageView.image) {
            if (self.backgroundImageView.alpha < 0.01) {
                return nil;
            }
        } else if (self.fakeView.alpha < 0.01) {
            return nil;
        }
    }
    
    if (CGRectEqualToRect(view.bounds, CGRectZero)) {
        return nil;
    }
    
    return view;
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    self.fakeView.frame = self.fakeView.superview.bounds;
    self.backgroundImageView.frame = self.backgroundImageView.superview.bounds;
    self.shadowImageView.frame = CGRectMake(0, CGRectGetHeight(self.shadowImageView.superview.bounds) - self.shadowImageViewOffsetY, CGRectGetWidth(self.shadowImageView.superview.bounds), self.shadowImageViewHeight);
    
    if (self.shadowImageViewOffsetY < self.shadowImageViewHeight){
        self.clipsToBounds = false;
    }
}


@end
